﻿#include <iostream>
#include<vector>
#include <string>
#include <stdio.h>
//#include <math.h>
#include<time.h>
#include"Allsort.h"
using namespace std;
#define random(x) (rand()%x)
#define BigrandomInt(x) (int(rand()*1.0/(RAND_MAX/(float)x)))
#define BigrandomFloat(x) (rand()*1.0/(RAND_MAX/(float)x))

void qsort(int a[],int left, int right)
{
	int i, j, t, temp;
	if (left > right)
		return ;
	temp = a[left];
	i = left;
	j = right;
	while (i != j)
	{
		while (a[j] >= temp&&i < j)j--;
		while (a[i] <= temp&&i < j)i++;
		if (i < j)
		{
			t = a[i];
			a[i] = a[j];
			a[j] = t;
		}
	}
	a[left] = a[i];
	a[i] = temp;
	qsort(a,left, i - 1);
	qsort(a,i + 1, right);
}

void quickSort(int s[], int l, int r)
{
	if (l < r)
	{
		int i = l, j = r, x = s[l];
		while (i < j)
		{
			while (i < j && s[j] >= x) // 从右向左找第一个小于x的数  
				j--;
			if (i < j)
				s[i++] = s[j];
			while (i < j && s[i] < x) // 从左向右找第一个大于等于x的数  
				i++;
			if (i < j)
				s[j--] = s[i];
		}
		s[i] = x;
		quickSort(s, l, i - 1); // 递归调用  
		quickSort(s, i + 1, r);
	}
}

/*int main()
{
	Allsort* sortClass = new Allsort;
	//int a[1000];
	int totalnum;
	while (1)
	{
		cin >> totalnum;
		if (totalnum != 0)
		{
			int *a = new int[totalnum];
			for (int i = 0; i < totalnum; i++)
			{
				cin >> a[i];
			}
			sortClass->BubbleSort(a, totalnum);
			sortClass->printArray(a, totalnum);
			delete[] a;
		}
		else{
			break;
		}
	}
	return 0;
}*/

#define LIST_NUM 100
#define NUM_LISTMAXNUM 10000
void main()
{
	int BubbleSortTime ,QuickSortTime;
	Allsort* sortClass = new Allsort;
	int a[LIST_NUM];
	srand((int)time(0));
	for (int i = 0; i < LIST_NUM; i++)
	{
		a[i] = random(NUM_LISTMAXNUM);
	}
	cout << "--------------------------原始数据--------------------" << endl;
	sortClass->printArray(a, countof_macro(a),1); 

	int b[LIST_NUM] ;
	memcpy(b, a, sizeof(a));
	clock_t timeBegin = clock();
	sortClass->BubbleSort(b, countof_macro(b));
	cout << "-------冒泡消耗时间 = " << (clock()-timeBegin) << endl << endl;
	BubbleSortTime = clock() - timeBegin;
	cout << "--------------------------冒泡排序后数据--------------------" << endl;
	sortClass->printArray(b, countof_macro(b), BUBBLE_SORT);
	cout<<endl;

	memcpy(b, a, sizeof(a));
	timeBegin = clock();
	sortClass->QuickSort(b, 0, countof_macro(b)-1);
	cout << "-------快速排序消耗时间 = " << (clock() - timeBegin) << endl << endl;
	QuickSortTime = clock() - timeBegin;
	cout << "--------------------------快速排序后数据--------------------" << endl;
	sortClass->printArray(b, countof_macro(b), QUICK_SORT);
	cout << endl;
	
	memcpy(b, a, sizeof(a));
	timeBegin = clock();
	sortClass->InsertionSort(b,countof_macro(b));
	cout << "-------代码简洁版插入排序消耗时间 = " << (clock() - timeBegin) << endl << endl;
	QuickSortTime = clock() - timeBegin;
	cout << "--------------------------代码简洁版插入排序后数据--------------------" << endl;
	sortClass->printArray(b, countof_macro(b), INSERT_SORT_0);
	cout << endl;

	memcpy(b, a, sizeof(a));
	timeBegin = clock();
	sortClass->Insertsort1(b, countof_macro(b));
	cout << "-------严格定义版插入排序消耗时间 = " << (clock() - timeBegin) << endl << endl;
	QuickSortTime = clock() - timeBegin;
	cout << "--------------------------严格定义版插入排序后数据--------------------" << endl;
	sortClass->printArray(b, countof_macro(b), INSERT_SORT_1);
	cout << endl;

	memcpy(b, a, sizeof(a));
	timeBegin = clock();
	sortClass->Shellsort(b, countof_macro(b));
	cout << "-------严格定义版希尔排序消耗时间 = " << (clock() - timeBegin) << endl << endl;
	QuickSortTime = clock() - timeBegin;
	cout << "--------------------------严格定义版希尔排序后数据--------------------" << endl;
	sortClass->printArray(b, countof_macro(b), SHELL_SORT_0);
	cout << endl;

	memcpy(b, a, sizeof(a));
	timeBegin = clock();
	sortClass->ShellsortSimple(b, countof_macro(b));
	cout << "-------代码简洁版希尔排序消耗时间 = " << (clock() - timeBegin) << endl << endl;
	QuickSortTime = clock() - timeBegin;
	cout << "--------------------------代码简洁版希尔排序后数据--------------------" << endl;
	sortClass->printArray(b, countof_macro(b), SHELL_SORT_1);
	cout << endl;

	system("pause");
}

